package com.tms.ned.controlmonitor.adapters;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;

import com.tms.ned.controlmonitor.ComponentChangeEvent;
import com.tms.ned.controlmonitor.validate.Validator;

/**
 * An adapter for a JCheckBox that can be registered with a {@link ControlMonitor}
 * @author mjmclaughlin
 */
public class CheckBoxAdapter extends AbstractComponentAdapter {

	private JCheckBox checkbox;
	private boolean initialSelect;
	
	private ItemListener internalItemListener = new ItemListener() {
		public void itemStateChanged(ItemEvent e) {
			ComponentChangeEvent event = new ComponentChangeEvent(CheckBoxAdapter.this,CheckBoxAdapter.this);
			fireComponentChangeEvent(event);
		}
	};
	
	public CheckBoxAdapter(String name, Component component, Validator validator) {
		super(name,component,validator);
		checkbox = (JCheckBox)component;
		checkbox.addItemListener(internalItemListener);
		setInitialValue((Boolean.valueOf(checkbox.isSelected())));
		
	}
	@Override
	protected void changeInitialValue(Object o) {
		this.initialSelect = Boolean.valueOf(o.toString());
		internalItemListener.itemStateChanged(null);
		
	}

	public Object getCurrentValue() {
		return Boolean.valueOf(checkbox.isSelected());
	}

	public Object getInitialValue() {
		return Boolean.valueOf(initialSelect);
	}

}
